import {
  Odometer,
  List,
  Wallet,
  ChatDotRound,
  Star,
  Shop,
  Setting,
  TrendCharts,
  Message,
  User,
  QuestionFilled,
  Ticket,
  DataAnalysis,
  Monitor,
  Files,
  Management,
  Tools,
  Bell,
  Shield,
  CreditCard,
  Location,
  Timer,
  Promotion
} from '@element-plus/icons-vue'

// 菜单配置
export const menuConfig = [
  {
    id: 'dashboard',
    title: '工作台',
    icon: Odometer,
    path: '/main/dashboard',
    type: 'item',
    badge: null,
    permissions: ['dashboard:view']
  },
  {
    id: 'business',
    title: '业务管理',
    icon: Management,
    type: 'group',
    children: [
      {
        id: 'orders',
        title: '订单管理',
        icon: List,
        path: '/main/orders',
        type: 'item',
        badge: 'urgent',
        permissions: ['orders:view']
      },
      {
        id: 'services',
        title: '服务管理',
        icon: Star,
        path: '/main/services',
        type: 'item',
        permissions: ['services:view']
      },
      {
        id: 'customers',
        title: '客户管理',
        icon: User,
        path: '/main/customers',
        type: 'item',
        permissions: ['customers:view']
      },
      {
        id: 'reviews',
        title: '评价管理',
        icon: ChatDotRound,
        path: '/main/reviews',
        type: 'item',
        permissions: ['reviews:view']
      }
    ]
  },
  {
    id: 'finance',
    title: '财务管理',
    icon: Wallet,
    type: 'group',
    children: [
      {
        id: 'finance-overview',
        title: '财务概览',
        icon: Wallet,
        path: '/main/finance',
        type: 'item',
        permissions: ['finance:view']
      },
      {
        id: 'deposit',
        title: '保证金管理',
        icon: Shield,
        path: '/main/deposit',
        type: 'item',
        permissions: ['deposit:view']
      },
      {
        id: 'withdrawal',
        title: '提现管理',
        icon: CreditCard,
        path: '/main/withdrawal',
        type: 'item',
        permissions: ['withdrawal:view']
      }
    ]
  },
  {
    id: 'marketing',
    title: '营销推广',
    icon: Promotion,
    type: 'group',
    children: [
      {
        id: 'coupons',
        title: '优惠券管理',
        icon: Ticket,
        path: '/main/coupons',
        type: 'item',
        permissions: ['coupons:view']
      },
      {
        id: 'promotions',
        title: '推广活动',
        icon: Promotion,
        path: '/main/promotions',
        type: 'item',
        permissions: ['promotions:view']
      }
    ]
  },
  {
    id: 'operations',
    title: '运营管理',
    icon: Shop,
    type: 'group',
    children: [
      {
        id: 'business-settings',
        title: '营业管理',
        icon: Shop,
        path: '/main/business',
        type: 'item',
        permissions: ['business:view']
      },
      {
        id: 'area-management',
        title: '服务区域',
        icon: Location,
        path: '/main/service-areas',
        type: 'item',
        permissions: ['areas:view']
      },
      {
        id: 'schedule',
        title: '营业时间',
        icon: Timer,
        path: '/main/schedule',
        type: 'item',
        permissions: ['schedule:view']
      }
    ]
  },
  {
    id: 'data',
    title: '数据分析',
    icon: TrendCharts,
    type: 'group',
    children: [
      {
        id: 'statistics',
        title: '数据统计',
        icon: DataAnalysis,
        path: '/main/statistics',
        type: 'item',
        permissions: ['statistics:view']
      },
      {
        id: 'reports',
        title: '报表中心',
        icon: Files,
        path: '/main/reports',
        type: 'item',
        permissions: ['reports:view']
      },
      {
        id: 'analytics',
        title: '经营分析',
        icon: Monitor,
        path: '/main/analytics',
        type: 'item',
        permissions: ['analytics:view']
      }
    ]
  },
  {
    id: 'communication',
    title: '沟通交流',
    icon: Message,
    type: 'group',
    children: [
      {
        id: 'messages',
        title: '消息中心',
        icon: Message,
        path: '/main/messages',
        type: 'item',
        badge: 'new',
        permissions: ['messages:view']
      },
      {
        id: 'notifications',
        title: '系统通知',
        icon: Bell,
        path: '/main/notifications',
        type: 'item',
        permissions: ['notifications:view']
      }
    ]
  },
  {
    id: 'system',
    title: '系统设置',
    icon: Setting,
    type: 'group',
    children: [
      {
        id: 'account',
        title: '账户中心',
        icon: User,
        path: '/main/account',
        type: 'item',
        permissions: ['account:view']
      },
      {
        id: 'settings',
        title: '商家设置',
        icon: Setting,
        path: '/main/settings',
        type: 'item',
        permissions: ['settings:view']
      },
      {
        id: 'tools',
        title: '工具箱',
        icon: Tools,
        path: '/main/tools',
        type: 'item',
        permissions: ['tools:view']
      },
      {
        id: 'help',
        title: '帮助中心',
        icon: QuestionFilled,
        path: '/main/help',
        type: 'item',
        permissions: []
      }
    ]
  }
]

// 菜单权限映射
export const menuPermissions = {
  'dashboard:view': '查看仪表板',
  'orders:view': '查看订单',
  'services:view': '查看服务',
  'customers:view': '查看客户',
  'reviews:view': '查看评价',
  'finance:view': '查看财务',
  'deposit:view': '查看保证金',
  'withdrawal:view': '查看提现',
  'coupons:view': '查看优惠券',
  'promotions:view': '查看推广',
  'business:view': '查看营业设置',
  'areas:view': '查看服务区域',
  'schedule:view': '查看营业时间',
  'statistics:view': '查看统计',
  'reports:view': '查看报表',
  'analytics:view': '查看分析',
  'messages:view': '查看消息',
  'notifications:view': '查看通知',
  'account:view': '查看账户',
  'settings:view': '查看设置',
  'tools:view': '查看工具'
}

// 菜单徽章类型
export const badgeTypes = {
  urgent: { type: 'danger', text: '紧急' },
  new: { type: 'primary', text: '新' },
  hot: { type: 'warning', text: '热' },
  update: { type: 'success', text: '更新' }
}

// 获取用户菜单（根据权限过滤）
export function getUserMenus(userPermissions = []) {
  const filterMenus = (menus) => {
    return menus.filter(menu => {
      // 如果是分组，递归过滤子菜单
      if (menu.type === 'group' && menu.children) {
        menu.children = filterMenus(menu.children)
        return menu.children.length > 0
      }
      
      // 如果是菜单项，检查权限
      if (menu.type === 'item') {
        // 如果没有权限要求，或者用户有相应权限，则显示
        return !menu.permissions?.length || 
               menu.permissions.some(permission => userPermissions.includes(permission))
      }
      
      return true
    })
  }
  
  return filterMenus(JSON.parse(JSON.stringify(menuConfig)))
}

// 获取菜单路径映射
export function getMenuPathMap() {
  const pathMap = {}
  
  const traverse = (menus) => {
    menus.forEach(menu => {
      if (menu.path) {
        pathMap[menu.path] = menu
      }
      if (menu.children) {
        traverse(menu.children)
      }
    })
  }
  
  traverse(menuConfig)
  return pathMap
}

// 根据路径查找菜单
export function findMenuByPath(path) {
  const pathMap = getMenuPathMap()
  return pathMap[path] || null
}
